package com.xxx.service.impl;

import com.xxx.dao.OrderDao;
import com.xxx.dao.OrderItemDao;
import com.xxx.pojo.Order;
import com.xxx.pojo.OrderItem;
import com.xxx.service.OrderService;
import com.xxx.service.ex.InsertException;
import com.xxx.service.ex.UpdatException;
import com.xxx.vo.OrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;

    //创建订单功能
    @Override
    public void creatOrder(Order order,String username) {
        order.setStatus(0);//状态0表示未支付
        order.setCreatedTime(new Date());
        order.setCreatedUser(username);
        order.setModifiedUser(username);
        order.setModifiedTime(new Date());
        order.setOrderTime(new Date());
        int row = orderDao.insert(order);
        if(row!=1){
            throw new InsertException("(创建订单操作时)插入数据时出现未知异常");
        }
    }

    //获取用户所有订单功能
    @Autowired
    private OrderItemDao orderItemDao;
    @Override
    public List<List<OrderItemVo>> getOrderItemVoList(Integer uid,Integer status) {
        Map<String, Object> map = new HashMap<>();
        if(status==-1) {
            //通过uid 获取用户所有订单编号

            map.put("uid", uid);
            List<Order> orderList = orderDao.selectByMap(map);
            //list 存储要返回的OrderItemVo集合
            List<List<OrderItemVo>> list = new ArrayList<>();
            for (Order order : orderList) {
                //通过各个订单编号获取 单个订单信息
                map.clear();
                map.put("order_num", order.getOrderNum());
                List<OrderItem> orderItem = orderItemDao.selectByMap(map);
                if (orderItem.size() == 0) {
                    continue;
                }
                //把需要的数据存到单个订单对象
                List<OrderItemVo> orderItemVoList = new ArrayList<>();
                for (OrderItem item : orderItem) {
                    OrderItemVo orderItemVo = new OrderItemVo();
                    orderItemVo.setOrderNum(order.getOrderNum());
                    orderItemVo.setCreatedTime(order.getCreatedTime());
                    orderItemVo.setRecvName(order.getRecvName());
                    orderItemVo.setRecvAddress(order.getRecvAddress());
                    orderItemVo.setId(item.getId());
                    orderItemVo.setImage(item.getImage());
                    orderItemVo.setTitle(item.getTitle());
                    orderItemVo.setPrice(item.getPrice());
                    orderItemVo.setNum(item.getNum());
                    orderItemVo.setStatus(item.getStatus());
                    //加入到集合
                    orderItemVoList.add(orderItemVo);
                }
                //加入到要返回的集合
                list.add(orderItemVoList);
            }
            return list;
        }else {//当status不为-1  表示查询的不是所有订单
            //通过uid 获取用户所有订单编号
            map.put("uid", uid);
            List<Order> orderList = orderDao.selectByMap(map);
            //list 存储要返回的OrderItemVo集合
            List<List<OrderItemVo>> list = new ArrayList<>();
            for (Order order : orderList) {
                //通过各个订单编号获取 单个订单信息
                map.clear();
                map.put("order_num", order.getOrderNum());
                map.put("status",status);
                List<OrderItem> orderItem = orderItemDao.selectByMap(map);
                if (orderItem.size() == 0) {
                    continue;
                }
                //把需要的数据存到单个订单对象
                List<OrderItemVo> orderItemVoList = new ArrayList<>();
                for (OrderItem item : orderItem) {
                    OrderItemVo orderItemVo = new OrderItemVo();
                    orderItemVo.setOrderNum(order.getOrderNum());
                    orderItemVo.setCreatedTime(order.getCreatedTime());
                    orderItemVo.setRecvName(order.getRecvName());
                    orderItemVo.setRecvAddress(order.getRecvAddress());
                    orderItemVo.setId(item.getId());
                    orderItemVo.setImage(item.getImage());
                    orderItemVo.setTitle(item.getTitle());
                    orderItemVo.setPrice(item.getPrice());
                    orderItemVo.setNum(item.getNum());
                    orderItemVo.setStatus(item.getStatus());
                    //加入到集合
                    orderItemVoList.add(orderItemVo);
                }
                //加入到要返回的集合
                list.add(orderItemVoList);
            }
            return list;
        }
    }

    //通过订单号 和单个订单id 查询单个订单的详细数据

    @Override
    public OrderItemVo getOneOrderItemVo(String orderNum, Integer id) {
        //通过订单编号查询 recvName recvTag phone recvAddress
        Map<String, Object> map=new HashMap<>();
        map.put("order_num",orderNum);
        List<Order> orders = orderDao.selectByMap(map);
        if(orders.size()==0){
            return null;
        }
        //存信息到OrderItemVo
        OrderItemVo orderItemVo=new OrderItemVo();
        orderItemVo.setOrderNum(orderNum);
        orderItemVo.setRecvName(orders.get(0).getRecvName());
        orderItemVo.setRecvTag(orders.get(0).getRecvTag());
        orderItemVo.setPhone(orders.get(0).getRecvPhone());
        orderItemVo.setRecvAddress(orders.get(0).getRecvAddress());
        //通过id 获取title image price num status creat_time
        OrderItem orderItem = orderItemDao.selectById(id);
        orderItemVo.setTitle(orderItem.getTitle());
        orderItemVo.setImage(orderItem.getImage());
        orderItemVo.setPrice(orderItem.getPrice());
        orderItemVo.setNum(orderItem.getNum());
        orderItemVo.setStatus(orderItem.getStatus());
        orderItemVo.setCreatedTime(orderItem.getCreatedTime());
        //返回单个订单信息
        return orderItemVo;
    }

    //确认收货功能实现
    @Override
    public void confirm(Integer[] ids) {
        for (Integer id : ids) {
            OrderItem orderItem=new OrderItem();
            orderItem.setId(id);
            orderItem.setStatus(4);
            int row = orderItemDao.updateById(orderItem);
            if(row!=1){
                throw new UpdatException("(确认收货操作时)更新时出现未知异常");
            }
        }
    }

//    //通过用户名uid  状态 status查询 待付款 待收货 待评价 退货退款的订单列表
//
//    @Override
//    public List<OrderItemVo> getOrderList(Integer uid,Integer status) {
//        Map<String, Object> map=new HashMap<>();
//        map.put("uid",uid);
//        map.put("status",status);
//        List<OrderItem> orderItems = orderItemDao.selectByMap(map);
//        if(orderItems.size()==0){
//            return null;
//        }
//        //要返回的集合
//        List<OrderItemVo> orderItemVoList=new ArrayList<>();
//        //设置单个OrderItemVo.
//        for (OrderItem orderItem : orderItems) {
//            OrderItemVo oneOrderItemVo = this.getOneOrderItemVo(orderItem.getOrderNum(), orderItem.getId());
//            oneOrderItemVo.setId(orderItem.getId());
//            orderItemVoList.add(oneOrderItemVo);
//        }
//        return orderItemVoList;
//    }
}
